package oop.ex1.actions;

import java.io.*;
import java.util.*;
import oop.ex1.filters.IllegalDateException;
import oop.ex1.filters.YesNoException;


/**
 * This class handles all the action processes in this program
 * @authors dawn16889 & miron_6
 */
public class ActionWork {
	
	/**
	 * this method creates a new Actions object and returns it
	 * @param ActionName - the name of the action
	 * @param s - the parameter that each action needs in order to do it's job
	 * @param sourceDir - the source directory
	 * @return a new Actions object 
	 * @throws IllegalDateException - if the date is in an illegal form
	 * @throws ActionException - if the name of the path is not accurate
	 * @throws YesNoException - if the given string is neither YES nor NO
	 */
	public static Actions getAction(String ActionName ,String s, String sourceDir)
								throws IllegalDateException, ActionException, YesNoException{
		try {
			eAction act = eAction.valueOf(ActionName);
			switch (act){
			case print_data:
				return new Action.PrintData();
			case print_name:
				return new Action.PrintName();
			case copy:
				return new Action.Copy(s, sourceDir);
			case exec:
				return new Action.Exec(s);
			case write:
				return new Action.Write(s);
			case last_mod:
				return new Action.LastMod(s);
			default:
				return null;
			}
		}
		// if the there is an inaccurate name instead of the actionName   
		catch (IllegalArgumentException e) {
			throw new ActionException();
		}
	}
	

	/**
	 * activates the an arrayList of actions on the given treeSet
	 * @param tree - the treeSet that the actions are activated on
	 * @param actions - all the actions that are activated in this Block
	 * @throws IOException - thrown by doAction
	 */
	public static void doActions(TreeSet<File> tree, ArrayList<Actions> actions) throws IOException{
		for(int i=0; i<actions.size(); i++){							
			Iterator<File> iter = tree.iterator();
			while(iter.hasNext()){
					((Actions)actions.get(i)).doAction(iter.next());
			}
		}
	}
}
